
***********************************************************************************************************************
**** Figure A2: Debt Accumulation *************************************************************************************
*********************************************************************************************************************


use "$data/Non_payment_dataset_ready.dta" , clear


 

xtset customer t

 forval i = 1(1)22  {
 bysort customer: egen cumulpayment`i'=sum(payment) if t <=`i' & t>=1
 bysort customer: egen cumulinvoice`i'=sum(invoices) if t <=`i' & t>=1
 }
gen accumulatedpayment=.
gen accumulatedinvoice=.

forval i = 1(1)22  {
replace accumulatedpayment=cumulpayment`i' if t==`i'
replace accumulatedinvoice=cumulinvoice`i' if t==`i'
}
replace accumulatedpayment=0 if accumulatedpayment==.
replace accumulatedinvoice=0 if accumulatedinvoice==.

gen tilgunggraph=accumulatedinvoice-accumulatedpayment

winsor tilgunggraph, gen(tilgunggraphw) p(0.01)


preserve
collapse (mean) tilgunggraphw, by(itt t)
tabulate itt, gen(g)
gen g4=0
replace g4=1 if itt==.

bysort itt: egen pretreatmentmean2=mean(tilgunggraphw) if t<13 // center around pre-treatment value
bysort itt: egen pretreatmentmean=mean(pretreatmentmean2)

bysort itt: gen firstperiod2=tilgunggraphw-pretreatmentmean if t==1
bysort itt: egen firstperiod=mean(firstperiod)

gen Basic=g1*tilgunggraphw-pretreatmentmean-firstperiod if itt==0
gen Selfconcept=g2*tilgunggraphw-pretreatmentmean-firstperiod if itt==1
gen Plan=g3*tilgunggraphw-pretreatmentmean-firstperiod if itt==2
gen untreated=g4*tilgunggraphw-pretreatmentmean-firstperiod if itt==.



line untreated Basic Selfconcept Plan t, xline(13, lwidth(tiny)) ytitle("Debt accumulation over time") graphregion(color(white)) ///
legend(order(1 "Untreated" 2 "Basic" 3 "Self-concept" 4 "Plan")) legend(col(4))  xtitle("") ///
xlabel(1 "Sept. 14"2 "Oct. 14" 3 "Nov. 14" 4 "Dec. 14" 5 "Jan. 15" 6 "Feb.15" 7 "March 15" 8 "April 15" 9 "May 15" 10 "June 15" 11 "July 15" 12 "Aug. 15" 13 "Sept. 15" 14 "Oct. 15" 15 "Nov. 15" 16 "Dec. 15" 17 "Jan. 16" 18 "Feb. 16" 19 "March 16" 20 "April 16" 21 "May 16" 22 "June 16", angle(90) labsize(small)) 

graph export "$filepath/output_logs/figure A2.pdf", replace
